<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item v-if="title == '新增'" label="订单号" :required="true" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-request ref="dropData" url="/procurement/purchaseOrder/listWSH" v-model="dataList.orderOdd" type="list" :trigger-change="true" @change="getData" />
                    </a-form-item>
                    <a-form-item v-else label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.orderOdd" placeholder="请输入订单号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="收货号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入收货号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="收货日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择收货日期" v-decorator="['goodsDate']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.supplier" placeholder="请输入供应商"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-tag type="list" v-decorator="['warehouseId', validatorRules.warehouseId]" :trigger-change="true" dictCode="bas_warehouse,name,id" @sendName="getName" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['invoiceType']" placeholder="请输入发票类型"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.contact" placeholder="请输入联系人"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.contactPhone" placeholder="请输入联系电话"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="付款日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择付款日期" v-decorator="['paymentDate']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['invoiceAmount']" placeholder="请输入开票金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="供应商欠款" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['arrearsOfSuppliers']" placeholder="请输入供应商欠款"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="凭证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['certificateNumber']" placeholder="请输入凭证号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.salesman" placeholder="请输入业务员"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="摘要" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.digest" placeholder="请输入摘要"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
    getAction
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'

export default {
    name: 'PurchaseReceiptSheetModal',
    components: {
        JDate,
    },
    //通过接口生成的收货单号
    props: ['odd'],
    data() {
        return {
            form: this.$form.createForm(this),
            title: '操作',
            width: 800,
            visible: false,
            model: {},
            dataList: {},
            labelCol: {
                sm: {
                    span: 6,
                },
            },
            wrapperCol: {
                sm: {
                    span: 16,
                },
            },

            confirmLoading: false,
            validatorRules: {
                warehouseId: {
                    rules: [{
                        required: true,
                        message: '请选择仓库!'
                    }]
                },
            },
            url: {
                add: '/procurement/purchaseReceiptSheet/add',
                edit: '/procurement/purchaseReceiptSheet/edit',
            },
        }
    },
    watch: {
        odd() {
            if (this.odd) {
                this.$set(this.dataList, 'odd', this.odd)
            }
        },
    },
    created() {},
    methods: {
        getName(value) {
            this.$set(this.dataList, 'warehouse', value)
        },
        async getData(value) {
            this.$set(this.dataList, 'orderOdd', value)
            let url = '/procurement/purchaseOrder/list'
            let params = {
                odd: value,
            }
            let result = await getAction(url, params)
            let {
                dataList
            } = this
            let {
                supplier,
                supplierId,
                contact,
                contactPhone,
                salesman,
                digest
            } = result.result.records[0]
            this.$set(dataList, 'supplier', supplier)
            this.$set(dataList, 'supplierId', supplierId)
            this.$set(dataList, 'contact', contact)
            this.$set(dataList, 'contactPhone', contactPhone)
            this.$set(dataList, 'salesman', salesman)
            this.$set(dataList, 'digest', digest)
        },
        add() {
            this.edit({})
        },
        edit(record) {
            this.form.resetFields()
            let {
                orderOdd,
                supplier,
                warehouse,
                contact,
                contactPhone,
                salesman,
                digest
            } = record
            this.dataList = {
                orderOdd,
                supplier,
                warehouse,
                contact,
                contactPhone,
                salesman,
                digest,
            }
            this.odd && !record.orderOdd ?
                this.$set(this.dataList, 'odd', this.odd) :
                this.$set(this.dataList, 'odd', record.odd)
            this.model = Object.assign({}, record)
            this.visible = true
            this.$nextTick(() => {
                this.form.setFieldsValue(
                    pick(
                        this.model,
                        'goodsDate',
                        'warehouseId',
                        'invoiceType',
                        'paymentDate',
                        'invoiceAmount',
                        'arrearsOfSuppliers',
                        'certificateNumber'
                    )
                )
            })
        },
        close() {
            this.$emit('close')
            this.visible = false
        },
        handleOk() {
            const that = this
            // 触发表单验证
            if (this.dataList.orderOdd == '' || this.dataList.orderOdd == undefined) {
                this.$message.warning('请选择订单号!')
                return false;
            }
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let httpurl = ''
                    let method = ''
                    if (!this.model.id) {
                        httpurl += this.url.add
                        method = 'post'
                    } else {
                        httpurl += this.url.edit
                        method = 'put'
                    }
                    values = Object.assign(values, this.dataList)
                    let formData = Object.assign(this.model, values)
                    httpAction(httpurl, formData, method)
                        .then((res) => {
                            if (res.success) {
                                that.$message.success(res.message)
                                that.$emit('ok')
                            } else {
                                that.$message.warning(res.message)
                            }
                        })
                        .finally(() => {
                            that.confirmLoading = false
                            that.close()
                            that.$refs.dropData.sendData()
                        })
                }
            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(
                pick(
                    row,
                    'goodsDate',
                    'warehouseId',
                    'invoiceType',
                    'paymentDate',
                    'invoiceAmount',
                    'arrearsOfSuppliers',
                    'certificateNumber'
                )
            )
        },
    },
}
</script>
